<script type="text/html" template>
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/reset.css">
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/set/Apply.css">
    <link rel="stylesheet" href="{{ layui.setter.base }}style//css/transaction/add_form.css">
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/patrol/project_device_alert.css">
</script>
    <!-- 建立视图 -->
    <div id="testDiv"></div>
    <!--  -->
    <script id="template" type="text/html">
       <div class="layui-fluid">
        <div class="table-box">
            <ul class="layui-row">
                <li class="layui-col-md4 layui-col-sm4">
                    <div class="form layui-form-item">
                        <label class="layui-label-inlineblock  layui-form-label">工单名称：</label>
                        <div class="layui-input-block">
                            {{#  if(d.tcReqName != null){ }}
                                {{d.tcReqName}}
                            {{#  } else { }}
                            {{# } }}
                        </div>
                    </div>
                    <div class="form layui-form-item">
                        <label class="layui-label-inlineblock    layui-form-label">原所属公司：</label>
                        <div class="layui-input-block">
                            {{#  if(d.oldAzpName != null){ }}
                            {{d.oldAzpName}}
                            {{#  } else { }}
                            {{# } }}
                            
                        </div>
                    </div>
                    <div class="form layui-form-item">
                        <label class=" 
                        layui-label-inlineblock  layui-form-label">申请日期：</label>
                        <div class="layui-input-block">
                            {{#  if(d.fil10 != null){ }}
                            {{d.fil10}}
                            {{#  } else { }}
                            {{# } }}
                        </div>
                    </div>
                </li>
                <li class="layui-col-md4 layui-col-sm4">
                    <div class="
                        form layui-form-item">
                        <label class="layui-label-inlineblock    layui-form-label">工单编号：</label>
                        <div class="layui-input-block">
                            {{#  if(d.fil01 != null){ }}
                            {{d.fil01}}
                            {{#  } else { }}
                            {{# } }}
                        </div>
                    </div>
                    <div class="
                        form layui-form-item">
                        <label class="layui-label-inlineblock    layui-form-label">OA工单编号：</label>
                        <div class="layui-input-block">
                            {{#  if(d.fil02 != null){ }}
                            {{d.fil02}}
                            {{#  } else { }}
                            {{# } }}
                            
                        </div>
                    </div>
                    <div class="form layui-form-item">
                        <label class="layui-label-inlineblock layui-form-label">申请借调公司：</label>
                        <div class="layui-input-block">
                              &nbsp;&nbsp;
                            {{#  if(d.newAzpName != null){ }}
                            {{d.newAzpName}}
                            {{#  } else { }}
                            {{# } }}
                        </div>
                    </div>
                </li>
                <li class="layui-col-md4 layui-col-sm4">
                    <div class="form layui-form-item">
                        <label class="layui-label-inlineblock layui-form-label">工单状态：</label>
                        <div class="layui-input-block">
                            {{# if(d.fil05=="0"){ }}
                            <span>未提交</span> 
                            {{# } else if(d.fil05=="1"){ }}
                            <span>待审核</span> 
                            {{# } else if(d.fil05=="2"){ }}
                            <span>待执行</span> 
                            {{# } else if(d.fil05=="6"){ }}
                            <span>OA送审中</span>
                            {{# } else if(d.fil05=="9"){ }}
                            <span>归档已完成</span>
                            {{# } else if(d.fil05== null){ }}
                            {{# } }}
                        </div>
                    </div>
                    <div class="form layui-form-item">
                        <label class="layui-label-inlineblock layui-form-label">申请人：</label>
                        <div class="layui-input-block">
                            {{#  if(d.fil08 != null){ }}
                            {{d.fil08}}
                            {{#  } else { }}
                            {{# } }}
                        </div>
                    </div>
                    <div class="form layui-form-item">
                        <label class="layui-label-inlineblock layui-form-label">借调日期：</label>
                        <div class="layui-input-block">
                             <input type="text" name="fil12" id="timeData" autocomplete="off" class="layui-input"  style="border: 1px solid #eee;">
                         </div>
                    </div>
                </li>
            </ul>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">调拨说明:</label>
                <div class="layui-input-block">
                    <textarea name="desc" placeholder="请输入调拨说明" class="layui-textarea" disabled>
                        {{#  if(d.fil21 != null){ }}
                        {{d.fil21}}
                        {{#  } else { }}
                        {{# } }}
                    </textarea>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注：</label>
                <div class="layui-input-block">
                    <textarea name="desc" placeholder="请输入备注" class="layui-textarea" disabled>
                        {{#  if(d.fil07 != null){ }}
                        {{d.fil07}}
                        {{#  } else { }}
                        {{# } }}
                    </textarea>
                </div>
            </div>
        </div>
        <!-- 表格 -->
        <table class="layui-table" id="LAY-content-comm" lay-filter="LAY-content-comm"></table>
      </div>
    </script>

    <script id="template_one" type="text/html">
        <div class="layui-fluid layui-form">
         <div class="table-box">
             <ul class="layui-row">
                 <li class="layui-col-md4 layui-col-sm4">
                     <div class="form layui-form-item">
                         <label class="layui-label-inlineblock layui-form-label"><span style="font-size: 18px;color: red;position: relative;display: inline-block;line-height: 20px;vertical-align: middle;top:3px;">*</span>工单名称：</label>
                         <div class="layui-input-block">
                             {{#  if(d.tcReqName != null){ }}
                            <input type="text" name="tcReqName" autocomplete="off" class="layui-input" value='{{d.tcReqName}}' lay-verify="required" style="border:0px;">
                            {{#  } else { }}
                            {{# } }}
                         </div>
                     </div>
                     <div class="
                         form layui-form-item">
                         <label class="layui-label-inlineblock    layui-form-label">OA工单编号：</label>
                         <div class="layui-input-block">
                             {{#  if(d.fil02 != null){ }}
                             <input type="text" name="fil02" autocomplete="off" class="layui-input" value='{{d.fil02}}' style="display:none;">
                             {{d.fil02}}
                             {{#  } else { }}
                             {{# } }}
                             
                         </div>
                     </div>
                     <!-- 原 -->
                     <div class="form layui-form-item">
                         <label class=" 
                         layui-label-inlineblock  layui-form-label">申请日期：</label>
                         <div class="layui-input-block">
                             {{#  if(d.fil10 != null){ }}
                             <input type="text" name="" autocomplete="off" class="layui-input" value='{{d.fil10}}' style="display:none;">
                             {{d.fil10}}
                             {{#  } else { }}
                             {{# } }}
                         </div>
                     </div>
                 </li>
                 <li class="layui-col-md4 layui-col-sm4">
                     <div class="
                         form layui-form-item">
                         <label class="layui-label-inlineblock    layui-form-label">工单编号：</label>
                         <div class="layui-input-block">
                             {{#  if(d.fil01 != null){ }}
                             <input type="text" name="fil01" autocomplete="off" class="layui-input" value='{{d.fil01}}' style="display:none;" id="fil01">
                             {{d.fil01}}
                             {{#  } else { }}
                             {{# } }}
                         </div>
                     </div>
                     
                     <!-- OA -->
                     <div class="form layui-form-item">
                         <label class="layui-label-inlineblock  layui-form-label">原所属公司：</label>
                         <div class="layui-input-block">
                             {{#  if(d.oldAzpName != null){ }}
                             <input type="text" name="tcReqCoorig" id="tcReqCoorig" autocomplete="off" class="layui-input" value='{{d.oldazp01}}' style="display:none;">
                             {{d.oldAzpName}}
                             {{#  } else { }}
                             {{# } }}
                         </div>
                     </div>
                     <div class="form layui-form-item">
                         <label class="layui-label-inlineblock layui-form-label">申请借调公司：</label>
                         <div class="layui-input-block">
                               &nbsp;&nbsp;
                             {{#  if(d.newAzpName != null){ }}
                             <input type="text" name="tcReqCoalt" autocomplete="off" class="layui-input" value='{{d.newazp01}}' style="display:none;">
                             {{d.newAzpName}}
                             {{#  } else { }}
                             {{# } }}
                         </div>
                     </div>
                 </li>
                 <li class="layui-col-md4 layui-col-sm4">
                     <div class="form layui-form-item">
                         <label class="layui-label-inlineblock layui-form-label">工单状态：</label>
                         <div class="layui-input-block">
                             {{# if(d.fil05=="0"){ }}
                             <input type="text" name="fil05" autocomplete="off" class="layui-input" value='{{d.fil05}}' style="display:none;">
                             <span>未提交</span>
                             {{# } else if(d.fil05=="1"){ }}
                             <input type="text" name="fil05" autocomplete="off" class="layui-input" value='{{d.fil05}}' style="display:none;">
                             <span>待审核</span>
                             {{# } else if(d.fil05=="2"){ }}
                             <input type="text" name="fil05" autocomplete="off" class="layui-input" value='{{d.fil05}}' style="display:none;">
                             <span>待执行</span>
                             {{# } else if(d.fil05=="6"){ }}
                             <input type="text" name="fil05" autocomplete="off" class="layui-input" value='{{d.fil05}}' style="display:none;">
                             <span>OA送审中</span>
                             {{# } else if(d.fil05=="9"){ }}
                             <input type="text" name="fil05" autocomplete="off" class="layui-input" value='{{d.fil05}}' style="display:none;">
                             <span>归档已完成</span>
                             {{# } else if(d.fil05== null){ }}
                             {{# } }}
                         </div>
                     </div>
                     <div class="form layui-form-item">
                         <label class="layui-label-inlineblock layui-form-label">申请人：</label>
                         <div class="layui-input-block">
                             {{#  if(d.fil08 != null){ }}
                             <input type="text" name="fil08" autocomplete="off" class="layui-input" value='{{d.gen01}}' style="display:none;">{{d.fil08}}{{#  } else { }}{{# } }}
                         </div>
                     </div>
                     <div class="form layui-form-item">
                         <label class="layui-label-inlineblock layui-form-label">借调日期：</label>
                         <div class="layui-input-block">
                             <input type="text" name="fil12" id="timeData" autocomplete="off" class="layui-input" value='{{d.fil12}}'  style="border: 1px solid #eee;">
                         </div>
                     </div>
                 </li>
             </ul>
             <div class="layui-form-item layui-form-text">
                 <label class="layui-form-label">调拨说明:</label>
                 <div class="layui-input-block">
                     <textarea name="fil21" placeholder="请输入调拨说明" class="layui-textarea">{{#  if(d.fil21 != null){ }}{{d.fil21}}{{#  } else { }}{{# } }}</textarea>
                 </div>
             </div>
             <div class="layui-form-item layui-form-text">
                 <label class="layui-form-label">备注：</label>
                 <div class="layui-input-block">
                     <textarea name="fil07" placeholder="请输入备注" class="layui-textarea">{{#  if(d.fil07 != null){ }}{{d.fil07}}{{#  } else { }}{{# } }}</textarea>
                 </div>
             </div>
         </div>
         <!-- 表格 -->
         <button class="add layui-btn layui-btn-radius layui-btn-primary" style="margin-top:20px">
            <i class="add-icon layui-icon layui-icon-add-circle" style="font-size: 15px;"></i>
            <span class="btn-inner">添加备件</span>
        </button>
         <table class="layui-table" id="LAY-content-comm" lay-filter="LAY-content-comm"></table>
         <div class="footer layui-btn-containner">
            <button class="commit layui-btn layui-btn-radius layui-btn-primary" lay-submit
                lay-filter="LAY-submit-tj">提交</button>
            <button class="hold layui-btn layui-btn-radius layui-btn-primary" lay-submit
                lay-filter="LAY-submit-bc">保存</button>
            <button class="cancel layui-btn layui-btn-radius layui-btn-primary" lay-filter="qxButton" lay-submit>取消</button>
        </div>
       </div>
     </script>
<script type="text/html" id="toolbarDemo">
<div>
    <span class="table-edit" lay-event="edit"></span>
    <span class="table-delete" lay-event="del"></span>
</div>
</script>
    <style scoped>
 .layui-table th {
    font-size: 10px;
    color: #333333;
    text-align: center;
    padding: 0;
    font-weight: bold;
    line-height: 2.6rem;
    border: 0;
    background-color: #CCDAE3;
    height: 26px;
    line-height: 26px;
    padding: 9px 0 8px;
}

.layui-table td {
    border: 0;
    font-size: 9px;
    color: #666666;
    text-align: center;
    height: 25px;
    line-height: 25px;
    padding: 10px 0 7px;
}

tbody tr:nth-child(even) {
    background-color: #CCDAE3;
}

.layui-table {
    margin: 0;
}
.columnCustomBtn{
    position: relative;
    width: 26px;
    height: 26px;
    padding: 5px;
    line-height: 16px;
    margin-right: 10px;
    text-align: center;
    color: #333;
    border: 1px solid #ccc;
    cursor: pointer;
    -webkit-transition: .5s all;
    transition: .5s all;
}
</style>
<script type="text/javascript">
    layui.use(['admin', 'form', 'user', 'table', 'api', 'laytpl', 'utill', 'laydate', 'jquery', 'layer'], function () {
        var $ = layui.$,
            admin = layui.admin,
            view = layui.view,
            api = layui.api,
            table = layui.table,
            setter = layui.setter,
            laytpl = layui.laytpl,
            utill = layui.utill,
            laydate = layui.laydate,
            layer = layui.layer,
            form = layui.form,
            router = layui.router();
            var  href = JSON.parse(sessionStorage.getItem('layui-this'));
            var  fil05 = JSON.parse(sessionStorage.getItem('fil05'));
            $(href).addClass('layui-this');
        // 拿到路由上的参数
        var fil01 = router.search.fil01;
            admin.req({
                type: "post",
                url: api.host + api.FilFileBPSelectDetails_llp,
                method: 'POST',
                data: {fil01:fil01},
                success: function (data){     
                    var data = data.data;
                    if(data.fil12 == null){
                       data.fil12=''
                    }
                        if(fil05 != 0){
                        $(".footer").hide();
                    
                    }else{
                        $(".footer").show();
                    }
                    laytpl($("#template_one").html()).render(data, function (html) {
                        $("#testDiv").html(html);
                    });
                    // 借调日期 
                    laydate.render({
                        elem: '#timeData',
                        trigger: 'click',
                        type: 'datetime',
                    });
                    utill.columnCustom('#LAY-content-comm', "FilFileSPFimListVO", {
                            url: api.host + api.FilFileBPSelectFimIma,
                            method: 'POST',
                            where: { fim01:fil01 },
                            toolbar: {
                                toolbar: '#toolbarDemo',
                                title: '操作',
                                align: 'center'
                            },
                        });
                }
            });
           
            $('#testDiv').on('click','.btn-inner',function(){
               sessionStorage.setItem('tcReqCoalt', $("#tcReqCoorig").val())
                admin.popup({
                    title: '新增备品备件',
                    move: false,
                    area: ['600px', '300px'],
                    id: 'LAY-popup-content-tags',
                    success: function (layero, index) {
                        view(this.id).render('set/set_alert/alert')
                            .done(function () {
                                //监听提交
                                form.on('submit(LAY-submit)', function (data) {
                                    let val = $('#tcDetailNum').val();
                                    let res = $('#tcDetailNum').data('id');
                                    if(val > res){
                                        $('#tcDetailNum').attr('value','');
                                       layer.msg(`调拨数量不能大于库存数量,库存数量为${res}`,{time:4500});
                                    }else{
                                        var field = data.field; //获取提交的字段
                                        field.ima01 = field.ima01.split("/")[0]
                                        var data = {
                                            fim01: fil01,
                                            tcDetailNum: field.tcDetailNum,
                                            tcDetailFia01: field.ima01
                                        }
                                        admin.req({
                                            type: "post",
                                            url: api.host + api.addSpFim,
                                            method: 'POST',
                                            data: JSON.stringify(data),
                                            dataType: 'json',
                                            contentType: 'application/json',
                                            success: function (data) {
                                                table.reload('#LAY-content-comm');
                                                layer.msg(data.message);
                                            }
                                        });
                                        layer.close(index); //执行关闭 
                                    }
                                });
                            });
                    }
                });
           
            })
            table.on('tool(LAY-content-comm)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data;
                var layEvent = obj.event;
                var tr = obj.tr;
                if(layEvent == 'del'){
                    layer.confirm('确定删除此数据吗?', {title:'提示',move: false}, function(index,layero){
                    // $(this).remove()
                    var fil01 = $('#fil01').val();
                    var res = {
                        fil01: fil01,
                        fia01: data.tcDetailFia01
                    }
                    admin.req({
                        type: "post",
                        url: api.host + api.deleteSpFil,
                        method: 'POST',
                        data: JSON.stringify(res),
                        dataType: 'json',
                        contentType: 'application/json',
                        success: function (data) {
                         layer.msg(data.message);
                         table.reload('#LAY-content-comm');
                        }
                    });
                    layer.closeAll(index)
                    table.reload('#LAY-content-comm');
                })
                }else if(layEvent == 'edit'){
                    admin.popup({
                    title: '编辑备件信息',
                    area: ['600px', '300px'],
                    id: 'LAY-popup-content-tags',
                    success: function (layero, index){
                        view(this.id).render('set/set_alert/bianji')
                            .done(function () {
                                form.val("layuiadmin-form-tags", {
                                    "ima01": data.tcDetailFia01,
                                    "ima02":data.ima02,
                                    "ima021": data.ima021,
                                    "ima25": data.ima25,
                                    "tcDetailNum": data.tcDetailNum
                                })
                                $('.btn2').click(function(){
                                    layer.close(index);
                                })
                                form.on('submit(LAY-submit)', function (data) {
                                    var field = data.field; //获取提交的字段
                                    var data = {
                                        fim01: fil01,
                                        tcDetailNum: field.tcDetailNum,
                                        tcDetailFia01: field.ima01
                                    }
                                    admin.req({
                                        type: "post",
                                        url: api.host + api.updateSpFim,
                                        method: 'POST',
                                        data: JSON.stringify(data),
                                        dataType: 'json',
                                        contentType: 'application/json',
                                        success: function (data) {
                                        layer.msg(data.message);
                                        table.reload('#LAY-content-comm');
                                        }
                                    });
                                    layer.close(index); //执行关闭 
                                    table.reload('#LAY-content-comm');
                                });
                            });
                    }
                });
                    
                }
            })
            
            form.on('submit(LAY-submit-tj)', function (data) {
                var field = data.field;
                if(field.fil12 == ""){
                    layer.open({content:'请填写借调日期'});
                    return
                }
                if(field.fil21 == ""){
                   layer.open({content:'请填写调拨说明'});
                   return
                }
                if(field.fil07 == ""){
                   layer.open({content:'请填写备注'});
                   return
                }
                var table_data = table.cache['#LAY-content-comm'];
                var data = field;
                data.filFileSpFimParams = table_data;    
                utill.loading.show();
                admin.req({
                    url: api.host + api.FilFileBPUpdateFilIma,
                    type: "post",
                    data: JSON.stringify(data),
                    dataType: 'json',
                    contentType: 'application/json',
                    done: function (res) {
                        if(res.code == 0){
                            utill.loading.hide();
                        layer.msg(res.message, {
                            time: 1000 
                        }, function () {
                            location.hash = '/set/sparecommit'
                        });  
                    }else{
                        layer.msg(res.message)
                        utill.loading.hide();
                    }    
                    }
                });
            }) 
            form.on('submit(qxButton)', function (data) {
                  location.hash = '/set/sparecommit' 
            })
            form.on('submit(LAY-submit-bc)', function (data) {
                var field = data.field;
                var table_data = table.cache['#LAY-content-comm'];
                var data = field;
                data.filFileSpFimParams = table_data;
                admin.req({
                url: api.host + api.FilFileBPUpdateFilIma2,
                type: "post",
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: 'application/json',
                done: function (res) {
                    if(res.code == 0){
                        layer.msg(res.message, {
                            time: 1000 
                        }, function () {
                            location.hash = '/set/sparecommit'
                        });  
                    }
                }
            });
            })  
            // }
    })
</script>